package com.mingliqiye.minecraft.enchantment.conflict.common;

import io.javalin.Javalin;
import io.javalin.http.Context;
import java.util.Map;

public class WebApis {

    private final ConfigManager configManager;
    private final WebConfigCoreMeMethod webConfigCoreMeMethod;

    WebApis(ConfigManager configManager, WebConfigCoreMeMethod webConfigCoreMeMethod) {
        this.configManager = configManager;
        this.webConfigCoreMeMethod = webConfigCoreMeMethod;
    }

    public void loadApis(Javalin javalin) {
        javalin.get("/api/get/info", this::getInfo);
        javalin.get("/api/get/enchantments", this::getAllEnchantments);
        javalin.get("/api/get/client/config", this::getClientConfig);
        javalin.post("/api/get/test", this::getTest);
        javalin.post("/api/send/config", this::sendConfig);
    }

    public void getAllEnchantments(Context context) {
        context.json(Respose.builder(webConfigCoreMeMethod.getAllEnchantments()));
    }

    public void getClientConfig(Context context) {
        context.json(Respose.builder(webConfigCoreMeMethod.getConfig()));
    }

    public void getInfo(Context context) {
        context.json(
            Respose.builder(
                Map.of(
                    "LoaderVersion",
                    webConfigCoreMeMethod.getLoaderVersions(),
                    "LoaderName",
                    webConfigCoreMeMethod.getLoaderName(),
                    "GameVersions",
                    webConfigCoreMeMethod.getGameVersions(),
                    "ModVersions",
                    webConfigCoreMeMethod.getModVersions(),
                    "ModName",
                    webConfigCoreMeMethod.getModName()
                )
            )
        );
    }

    public void getTest(Context context) {
        record BD(Integer fid, Integer sid) {}
        var d = context.bodyAsClass(BD.class);
        context.json(Respose.builder(webConfigCoreMeMethod.getTest(d.fid, d.sid)));
    }

    public void sendConfig(Context context) {
        webConfigCoreMeMethod.saveConfig(context.bodyAsClass(ConfigManager.MODCONFIGTYPE));
        context.json(Respose.builder());
    }
}
